﻿using System;
using System.Reflection;
using System.Resources;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using c2iGMailNotifier.Messaging;
using c2iGMailNotifier.View;
using Microsoft.Win32;
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Taskbar;
using GalaSoft.MvvmLight.Messaging;
using c2iGMailNotifier.Properties;

namespace c2iGMailNotifier
{
    /// <summary>
    /// Logique d'interaction pour Window1.xaml
    /// </summary>
    public partial class Window1
    {

        public Window1()
        {
            InitializeComponent();

            if (!TaskbarManager.IsPlatformSupported)
            {
                MessageBox.Show(
                    "This demo application interacts with the Windows 7 Taskbar. The current operating system does not support this feature.");
                Application.Current.Shutdown();
            }

            Messenger.Default.Register(this,
                                       delegate(ShowViewMessage message)
                                       {
                                           ViewDefinition viewDefinition = UIManager.GetViewDefinition(message.ViewKey);
                                           tp.Content = viewDefinition.GetInstance();
                                           Messenger.Default.Send(new ViewActivatedMessage(message.Sender, message.Target) { Parameter = message.Parameter });
                                       }
                );

            UIManager.RegisterView("OptionsView", null, typeof(OptionsView));
            UIManager.RegisterView("EmailsListView", null, typeof(EmailsListView));
        }


        private ThumbnailToolbarButton btnRefresh;
        private ThumbnailToolbarButton btnGoToGMail;
        private JumpList jumplist;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            GlassHelper.ExtendGlassFrame(this);

            TaskbarManager.Instance.ApplicationId = "c2iGMailNotifier";

            // Refresh Button
            btnRefresh = new ThumbnailToolbarButton(Properties.Resources.RefreshBlue16, "Refresh");
            btnRefresh.Click += btnRefresh_Click;
            // GoToGMail Button
            btnGoToGMail = new ThumbnailToolbarButton(Properties.Resources.GoToGMail16, "Go to GMail");
            btnGoToGMail.Click += btnGoToGMail_Click;

            TaskbarManager.Instance.ThumbnailToolbars.AddButtons(new WindowInteropHelper(this).Handle, btnRefresh, btnGoToGMail);

            // Jumplist
            jumplist = JumpList.CreateJumpList();

            JumpListLink link = new JumpListLink(@"http://mail.google.com/mail", "GMail") { IconReference = new IconReference(Assembly.GetExecutingAssembly().Location, 0) };
            jumplist.AddUserTasks(link);
            jumplist.Refresh();

            // force loading OptionsView for checking update
            UIManager.GetView("OptionsView");

            if (string.IsNullOrEmpty(Service.SettingsReaderWriter.GetCurrentUser().UserName))
            {
                Messenger.Default.Send(new ShowViewMessage(this) { ViewKey = "OptionsView" });
            }
            else
            {
                Messenger.Default.Send(new ShowViewMessage(this) { ViewKey = "EmailsListView" });
            }

            Messenger.Default.Send(new RefreshMessage(this));
            Messenger.Default.Send(new CheckVersionMessage(this));
        }

        void btnGoToGMail_Click(object sender, ThumbnailButtonClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://mail.google.com/mail");
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            Messenger.Default.Send(new RefreshMessage(this));
        }

    }


}